<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Common\Controller;

/**
 * Description of AnalyzeConditionController
 *
 * @author caoch
 */
class AnalyzeConditionController {
    /*
     * 获取对照表明细
     */
    private function setContrastData($key,$rulesArr){

        $file=$key;
        $array=F($file);
        if(empty($array)){
            $sql='select '
                    .$rulesArr[$key]['ContrastId'].' as id'
                    .','
                    .$rulesArr[$key]['ContrastName'].' as title'
                    .' from '
                    .$rulesArr[$key]['ContrastSheet']
                    .' order by '
                    .$rulesArr[$key]['ContrastSort'];
            
            $data=M()->query($sql);
            
            $array=F($file,$data);
            return $data;
        }else{
            return $array;
        }
    }
    /*
     * 获取相关联的对照表关系数据
     */
    private function setRelevanceData($table,$rulesArr,$key){
        $CountRules=$rulesArr[$key]['CountRules'];
        $file=md5($key.$CountRules);
        
        $array=F($file);
        
        if(empty($array)){
            //1.当前关键字对照
            $id[]=$key;//ID字段
            $title[]=$rulesArr[$key]['ContrastSheet'].'.'.$rulesArr[$key]['ContrastName'].' as '.$key.'_T';//TITLE
            $inner[]=' left join '.$rulesArr[$key]['ContrastSheet'].' on '.$rulesArr[$key]['ContrastSheet'].'.'.$rulesArr[$key]['ContrastId'].'='.$key;
            $order[]=$key;
            
            //2.约束字段的对照
            $arr=  explode(',', $CountRules);
            foreach($arr as $val){
                $k=  strtoupper($val);
                switch ($rulesArr[$k]['FieldType']){
                    case 1:
                        $id[]=$k;//ID字段
                        $title[]=$rulesArr[$k]['ContrastSheet'].'.'.$rulesArr[$k]['ContrastName'].' as '.$k.'_T';//TITLE
                        $inner[]=' left join '.$rulesArr[$k]['ContrastSheet'].' on '.$rulesArr[$k]['ContrastSheet'].'.'.$rulesArr[$k]['ContrastId'].'='.$k;
                        $order[]=$rulesArr[$k]['ContrastSheet'].'.'.$rulesArr[$k]['ContrastSort'];
                        break;
                    case 2:
                    case 3:
                        break;
                }
            }
            
            $col = array_merge($id,$title);
            
            $rsql='select '.implode(',', $id).' from '.$table.' group by '.implode(',', $id);            
            
            $sql='select '
                    .implode(',', $col)
                    .' from ('
                    .$rsql
                    .') as t '
                    .implode(' ', $inner)
                    .' order by '
                    .implode(',', $order);
            
            $data=M()->query($sql);

            $array=F($file,$data);
            return $data;
        }else{
            return $array;
        }
    }

    /*
     * 方法：判断当前传入的参数是否有约束关系
     * 参数：$post数组,$rules字符串
     */
    private function isRelevance($post,$rules){
        $pkey=  array_keys($post);
        $sarr= explode(',', $rules);
        $r=array();
        
        foreach ($pkey as $val){
            if(in_array($val, $sarr)){
                $r[]=$val;
            }
        }
        
        if(empty($r)){
            return false;
        }else{
            return $r;
        }
    }

    private function checkData($key,$isRelevance,$post,$val){
        $num=count($isRelevance);
        $result=array();
        
        //逐行分析数据源
        foreach($val as $k=>$v){
            $i=0;
            //解析约束比较表达式
            foreach($isRelevance as $kk){//获取存在的约束字段
                if($v[$kk]==$post[$kk]){
                    $i += 1;
                }
            }
            if($i==$num){
                $id=$v[$key];
                if(!empty($id)){
                    $t = $key.'_T';
                    $title=$v[$t];

                    $result[]=array('id'=>$id,'title'=>$title);
                }
            }
        }
        
        return $result;//array_unique($result);
    }

    private function conditions($table,$con,$post){
        $file=$table.'_Rules';

        $rulesArr=F($file);
        
        $select=array();
        
        foreach($con as $val){
            $key=  strtoupper($val);
            switch ($rulesArr[$key]['FieldType']){
                case 1:
                    $select[$i]['id']=$key;
                    $select[$i]['value'][0]=array(
                                             'id'=>0,
                                             'title'=>'--请选择'.$rulesArr[$key]['FieldName'].'--'
                                            );
                    $result=array();
                    
                    if(!empty($rulesArr[$key]['CountRules'])){//若存在约束关系
                        if(!empty($post)){//若存在传入参数
                            $isRelevance=$this->isRelevance($post,$rulesArr[$key]['CountRules']);
                            if($isRelevance){//若存在约束
                                $data=$this->setRelevanceData($table,$rulesArr,$key);//var_dump($data);
                                $res = $this->checkData($key,$isRelevance,$post,$data);
                            }else{
                                $res = $this->setContrastData($key,$rulesArr);
                            }
                        }else{
                            $res = $this->setContrastData($key,$rulesArr);
                        }
                    }else{
                        $res = $this->setContrastData($key,$rulesArr);
                    }
                    
                                         
                    foreach($res as $qyval){
                        $select[$i]['value'][$qyval['id']]=array(
                            'id'=>$qyval['id'],
                            'title'=>$qyval['title']
                        ); 
                    }
                    break;
                case 2:
                    $input="<input type='text' id='".$key['min']."'>"."-"."<input type='text' id='".$key['max']."'>";
                    break;
                case 3:                    
                    $select[$i]['id']=$key;
                    $select[$i]['value'][0]=array(
                                             'id'=>0,
                                             'title'=>'--请选择'.$rulesArr[$key]['FieldName'].'--'
                                         );
                    $arr=  explode('|', $rulesArr[$key]['Expressions']);
                    
                    switch ($arr[0]){
                        case 'Y':
                            $max=date('Y');
                            for($m=2010;$m<=$max;$m++){
                                $select[$i]['value'][$m]=array(
                                     'id'=>$m,
                                     'title'=>$m.'年'
                                 );
                            }
                            break;
                        case 'YM':
                            for($m=1;$m<=12;$m++){
                                $select[$i]['value'][$m]=array(
                                     'id'=>$m,
                                     'title'=>'第'.$m.'月'
                                 );
                            }
                            break;
                        case 'YW':
                            for($m=1;$m<=52;$m++){
                                $select[$i]['value'][$m]=array(
                                     'id'=>$m,
                                     'title'=>'第'.$m.'周'
                                 );
                            }
                            break;
                        case 'YQ':
                            for($m=1;$m<=4;$m++){
                                $select[$i]['value'][$m]=array(
                                     'id'=>$m,
                                     'title'=>'第'.$m.'季度'
                                 );
                            }
                            break;
                    }
                }
            ++$i;
        }
        
        ksort($select);
        
        $this->assign('select',$select);
        $this->assign('post',$post);
    }
}
